<?php
/*
Auth:Sang
Desc:支付接口基类
Date:2014-11-01
*/
namespace Lib\Payment;
class Payment{
	// 支付完成
	const PAY_FINISHED = 1;

	// 等待发货
	const PAY_SHIPPED = 2;

	// 退款
	const PAY_REFUND = 3;

	// 交易取消
	const PAY_CANCELED = -1;

	protected $code;
	private static $code_static;
	private $payment;
	// 服务器通知回调地址
	private $notice_callback;
	// 支付成功时支付网关跳转到的地址
	private $url_callback;
	//支付失败时跳转到的地址
	private $error_callback='';
	protected $config;
	protected $error;
	private static $names = [
		'Alipay'=>'支付宝',
		'KuaiQian' => '快钱支付',
	];

	public function __construct(){
		$payment = get_called_class();
		$arr = explode('\\',$payment);
		$this->code = array_pop($arr);
		unset($arr);
		if($config = $this->checkPayment($this->code)){
			$this->config = $config;
			$this->setNoticeCallback(site_url().'/payCallback/'.lcfirst($this->getCode()).'/asyn');
			$this->setUrlCallback(site_url().'/payCallback/'.lcfirst($this->getCode()).'/syn');
			$this->setErrorCallback(site_url().'/payCallback/error');
		}else{
			throw new \Exception("支付接口 [".$this->getName()."] 未启用", 510);
		}
	}

	/*设置服务器通知回调地址*/
	public function setNoticeCallback($callback){
		$this->notice_callback = $callback;
	}

	/*获得服务器通知回调地址*/
	public function getNoticeCallback(){
		return $this->notice_callback;
	}

	/*设置URL回调地址*/
	public function setUrlCallback($callback){
		$this->url_callback = $callback;
	}

	/*获得URL回调地址*/
	public function getUrlCallback(){
		return $this->url_callback;
	}

	public function setErrorCallback($callback){
		$this->error_callback = $callback;
	}

	public function getErrorCallback(){
		return $this->error_callback;
	}

	public function getError(){
		return $this->error;
	}
	public static function getPayments(){
		$odir = opendir(__DIR__);
		$list = [];
		while($file = readdir($odir)){
			if($file{0}!='.' && is_dir(__DIR__.'/'.$file)){
				$list[$file] = [
					'payment_code'=>$file,
					'payment_name'=>self::$names[$file],
					'config'=>'',
					'is_online'=>1,
					'enabled'=>0,
					'sort_order'=>255
				];
			}
		}
		return $list;
	}

	/*检查接口*/
	public static function checkPayment($payment){
		if(empty($payment)){
			return false;
		}
		$payment_mod = model('#Payment');
		$ment = $payment_mod->getPaymentByCode($payment);
		if(empty($ment) || $ment['enabled']==0 || empty($ment['config'])){
			return false;
		}
		$pay_class = '\\Lib\\Payment\\'.$payment.'\\'.$payment;
		if(class_exists($pay_class)){
			return unserialize($ment['config']);
		}
		return false;
	}

	/*获取接口*/
	public static function getPayment($payment){
		$pay_class = '\\Lib\\Payment\\'.$payment.'\\'.$payment;
		if(class_exists($pay_class)){
			return new $pay_class;
		}
		return false;
	}
	
	public function getCode(){
		return $this->code;
	}
}

interface iPayment{
	public function setNoticeCallback($callback);
	public function getNoticeCallback();
	public function setUrlCallback($callback);
	public function getUrlCallback();
	public function createForm($order);
	public function verifyNotice($order);
	public function verifyReturn($order);
	public static function getName();
	public function getCode();
	public static function getConfigField();
}